package com.iaiai.notepad;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * 
 * <p>
 * Title: EditNotepad.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-12-4
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class EditNotepadActivity extends NewNotepadActivity {

	private ProgressDialog loadingDialog; // loading
	
	private Bundle bundle;

	protected void initListener() {
		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(!sdCardIsExsit()){
					Toast.makeText(EditNotepadActivity.this, "sdcard不存在或有写保护!", Toast.LENGTH_LONG).show();
					return;
				}
				
				save();	//保存文件
			}
		});
	}

	private void save() {
		Bundle bundle = getIntent().getExtras();
		String filename = bundle.getString("folder").replace("//", "/");

		final File file = new File(filename);
		try {
			byte[] b = content.getText().toString().getBytes();
			OutputStream out = new FileOutputStream(file);
			for (byte temp : b) {
				out.write(temp);
			}
			out.close();
		} catch (FileNotFoundException e) {
			Toast.makeText(EditNotepadActivity.this, "sdcard不存在或有写保护!", Toast.LENGTH_LONG).show();
			return;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Toast.makeText(EditNotepadActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
		finish();
	}
	
	protected void setView(){
		setContentView(R.layout.edit_notepad);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		loadingDialog = new ProgressDialog(this);
		loadingDialog.setMessage("Loading...");
		loadingDialog.setCancelable(false);
		bundle = getIntent().getExtras();
		final String filename = bundle.getString("folder").replace("//", "/");

		// 判断如果不是在sdcard上不能保存
		if (!filename.toLowerCase().startsWith("/sdcard")
				&& !filename.toLowerCase().startsWith("/mnt/sdcard")) {
			btnSave.setVisibility(View.INVISIBLE);
			Toast.makeText(this, "此文件不能编辑，只能编辑sdcard上的文件!", Toast.LENGTH_LONG)
					.show();
		}

		loadingDialog.show();
		final Handler handler = new Handler();
		new Thread() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						File file = new File(filename);
						title.setText(file.getName());
						showText(readFile(file));
						loadingDialog.cancel();
					}
				});
			}
		}.start();
	}
	
	/**
	 * 显示内容
	 * @param html
	 */
	private void showText(String html){
		Pattern pattern = Pattern.compile("<img src=\"(.*?)\">");
		Matcher matcher = pattern.matcher(html);
		while(matcher.find()){
			
		}
		content.setText(Html.fromHtml(html));
	}

	private String readFile(File file) {
		String ct = null;
		InputStream in = null;
		try {
			int length = (int) file.length();
			byte[] bt = new byte[length];
			int num = 0;

			// 一次读一个字节
			in = new FileInputStream(file);
			int tempbyte;
			while (num < length) {
				byte[] temp = new byte[1];
				in.read(temp, 0, 1);
				bt[num] = temp[0];
				num++;
			}
			in.close();
			ct = new String(bt);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ct;
	}
	
	/**
	 * 判断sdcard是否已安装
	 * 
	 * @return true安装，false未安装
	 */
	private boolean sdCardIsExsit() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

}
